package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.WbCitiesBo;
import org.dromara.system.domain.bo.WbDistrictsBo;
import org.dromara.system.domain.dto.WbProvinceDTO;
import org.dromara.system.domain.vo.WbCitiesVo;
import org.dromara.system.service.IWbCitiesService;
import org.dromara.system.service.IWbDistrictsService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbProvinceVo;
import org.dromara.system.domain.bo.WbProvinceBo;
import org.dromara.system.service.IWbProvinceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 省份
 *
 * @author Lion Li
 * @date 2024-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/province")
public class WbProvinceController extends BaseController {

    private final IWbDistrictsService wbDistrictsService;
    private final IWbProvinceService wbProvinceService;
    private final IWbCitiesService wbCitiesService;

    /**
     * 查询省份列表
     */
    @SaCheckPermission("system:province:list")
    @GetMapping("/list")
    public TableDataInfo<WbProvinceVo> list(WbProvinceBo bo, PageQuery pageQuery) {
        return wbProvinceService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询省份列表
     */
    @SaCheckPermission("system:province:list")
    @GetMapping("/listZYT")
    public TableDataInfo<WbProvinceVo> listZYT(WbProvinceDTO dto,PageQuery pageQuery) {
        return  wbProvinceService.queryListZYQ(dto,pageQuery);
    }


    /**
     * 导出省份列表
     */
    @SaCheckPermission("system:province:export")
    @Log(title = "省份", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbProvinceBo bo, HttpServletResponse response) {
        List<WbProvinceVo> list = wbProvinceService.queryList(bo);
        ExcelUtil.exportExcel(list, "省份", WbProvinceVo.class, response);
    }

    /**
     * 获取省份详细信息
     *
     * @param provinceId 主键
     */
    @SaCheckPermission("system:province:query")
    @GetMapping("/{provinceId}")
    public R<WbProvinceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long provinceId) {
        return R.ok(wbProvinceService.queryById(provinceId));
    }

    /**
     * 新增省份
     */
    @SaCheckPermission("system:province:add")
    @Log(title = "省份", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbProvinceBo bo) {
        if(bo.getType() == 1){
            return toAjax(wbProvinceService.insertByBo(bo));
        }else if (bo.getType() == 2){
            WbCitiesBo wbCitiesBo = new WbCitiesBo();
            wbCitiesBo.setCityId(bo.getProvinceId());
            wbCitiesBo.setCityName(bo.getProvinceName());
            wbCitiesBo.setZipCode(bo.getZipCode());
            wbCitiesBo.setProvinceId(bo.getFatherId());

            return toAjax(wbCitiesService.insertByBo(wbCitiesBo));
        }else if (bo.getType() == 3){
            WbDistrictsBo wbDistrictsBo = new WbDistrictsBo();
            wbDistrictsBo.setDistrictId(bo.getProvinceId());
            wbDistrictsBo.setDistrictName(bo.getProvinceName());
            wbDistrictsBo.setZipCode(bo.getZipCode());
            wbDistrictsBo.setCityId(bo.getFatherId());
            return toAjax(wbDistrictsService.insertByBo(wbDistrictsBo));
        }else {
            return R.fail();
        }

    }
    /**
     * 修改省份
     */
    @SaCheckPermission("system:province:edit")
    @Log(title = "省份", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbProvinceBo bo) {
        if(bo.getType() == 1){
            return toAjax(wbProvinceService.updateByBo(bo));
        }else if (bo.getType() == 2){
            WbCitiesBo wbCitiesBo = new WbCitiesBo();
            wbCitiesBo.setCityId(bo.getProvinceId());
            wbCitiesBo.setCityName(bo.getProvinceName());
            wbCitiesBo.setZipCode(bo.getZipCode());
            wbCitiesBo.setProvinceId(bo.getFatherId());
            return toAjax(wbCitiesService.updateByBo(wbCitiesBo));
        }else if (bo.getType() == 3){
            WbDistrictsBo wbDistrictsBo = new WbDistrictsBo();
            wbDistrictsBo.setDistrictId(bo.getProvinceId());
            wbDistrictsBo.setDistrictName(bo.getProvinceName());
            wbDistrictsBo.setZipCode(bo.getZipCode());
            wbDistrictsBo.setCityId(bo.getFatherId());
            return toAjax(wbDistrictsService.updateByBo(wbDistrictsBo));
        }else {
            return R.fail();
        }
    }

    /**
     * 删除省份
     *
     * @param provinceIds 主键串
     */
    @SaCheckPermission("system:province:remove")
    @Log(title = "省份", businessType = BusinessType.DELETE)
    @DeleteMapping("/{provinceIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] provinceIds ,Integer type) {
        if(type == 1 ){
            if (0 < wbProvinceService.querySonNum(List.of(provinceIds))){
                return R.fail("选择的省级中，还有市级存在");
            }else {
                return toAjax(wbProvinceService.deleteWithValidByIds(List.of(provinceIds), true));
            }
        }else if (type == 2){
            if (0 < wbCitiesService.querySonNum(List.of(provinceIds))){
                return R.fail("选择的市级中，还有县（区）级存在");
            }else {
                return toAjax(wbCitiesService.deleteWithValidByIds(List.of(provinceIds), true));
            }
        }else if (type == 3){
            return toAjax(wbDistrictsService.deleteWithValidByIds(List.of(provinceIds), true));
        }else {
            return R.fail();
        }

    }
}
